<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class LifesController extends AppController {

	public $name = 'Lifes';

	

	public $uses = array('Image','ImageCategory','News','NewsCategory');
	public $layout='default';
	

	public function index($cid=0,$page=0) {	
		//init data
		
		$cid=intval($cid);
		$page=intval($page);
		$locale=Configure::read('Config.language');
		
		if(!$this->Session->check('Life')){
			//khoi tao array vao bien $tmp
			$tmp=array(
				'limit'=>8,
				'conditions'=>'',
				'fields'=>null,
				'order'=>'Life.id',
				'page'=>1,
				'recursive'=>0,
				'sortField'=>null,
				'sortDir'=>'asc',				
				'filterField'=>'',
				'filterText'=>'',
				'catid'=>'',
			);
			//luu vao session
			$this->Session->write('Life',$tmp);			
		}
		$this->Session->write('Life.page',$page);
		
		//progress data
		
		$conditions=$this->Session->read('Life.conditions');
		$this->Image->locale=$locale;
		$this->ImageCategory->locale=$locale;
		$conditions['ImageCategory.published']=1;
		$imagecat=$this->ImageCategory->read(null,$cid);
		if(!empty($imagecat)){
			$catname=$imagecat['ImageCategory']['title'];
			$conditions['Image.image_category_id']=$cid;
		}
		else{
			$catname=_resource('Our beautiful life',true);
			unset($conditions['Image.image_category_id']);
		}
		//update conditions
		$this->Session->write('Life.conditions',$conditions);
		if($page==0){
			$this->Session->write('Life.order','Image.id desc');		
		}
		else 
			$this->Session->write('Life.order','Image.id');	
		$this->paginate=$this->Session->read('Life');
		
		$images=$this->paginate('Image');
		$imagecats=$this->ImageCategory->find('all',array('conditions'=>'ImageCategory.published=1','order'=>'ImageCategory.id desc','limit'=>8));
		
		//send data to view
		$this->set('images',$images);
		$this->set('imagecats',$imagecats);
		$this->set('catname',$catname);
		$this->set('locale',$locale);
		$this->set('cid',$cid);
		$this->set('page',$page);
		$this->set('css','home,life');
		
		//for seo
		$this->set('title_for_layout',$catname);
		$this->set('metakeyword',$catname);
		$this->set('metadescription',$catname);
		$this->set('ogTitle',$catname);
		$this->set('ogType','article');
		$this->set('ogDescription',$catname);
		
		//$this->redirect("homes/home");
	}
	
	public function detail($id=0) {	
		$locale=Configure::read('Config.language');
		$id=intval($id);
		$catname='';
		$data=$this->Image->find('first',array('conditions'=>'Image.published=1 and Image.id='.$id));
		if(!empty($data)){
			$datacat=$this->{$this->uses[1]}->find('first',array('conditions'=>$this->uses[1].'.id='.$data[$this->uses[0]]['image_category_id']));
			if(isset($datacat[$this->uses[1]]['title']))$catname=$datacat[$this->uses[1]]['title'];
			$numImage=3;
			//get next 5 image
			$nextImages=$this->Image->find('all',array('conditions'=>'ImageCategory.published=1 and Image.image_category_id='.$data['Image']['image_category_id'].' and Image.id>'.$id,'order'=>'Image.id','limit'=>$numImage+1));
			//get prev 5 image
			$prevImages=$this->Image->find('all',array('conditions'=>'ImageCategory.published=1 and Image.image_category_id='.$data['Image']['image_category_id'].' and Image.id<'.$id,'order'=>'Image.id desc','limit'=>$numImage));
			//check lack image
			
			if(count($nextImages)<$numImage+1){
				//get more prevImage
				$prevImages=$this->Image->find('all',array('conditions'=>'ImageCategory.published=1 and Image.image_category_id='.$data['Image']['image_category_id'].' and Image.id<'.$id,'order'=>'Image.id desc','limit'=>$numImage*2+1-count($nextImages)));
			}
			else if(count($prevImages)<$numImage){
				//get more nextImage
				$nextImages=$this->Image->find('all',array('conditions'=>'ImageCategory.published=1 and Image.image_category_id='.$data['Image']['image_category_id'].' and Image.id>'.$id,'order'=>'Image.id','limit'=>$numImage*2+1-count($prevImages)));
			}
			
			//get news relate to this image:			
			$conditions['News.published']=1;
			$OR=array();
			if(!empty($data['Image']['tag'])){
				foreach(explode(',',$data['Image']['tag']) as $k=>$v){	
					if(!empty($v)){						
						$OR['left(News.tag,LENGTH(\''.$v.'\')+1)']=$v.',';
						$OR['right(News.tag,LENGTH(\''.$v.'\')+1)']=','.$v;
						$OR['left(News.tag,LENGTH(News.tag)-LENGTH(\''.$v.'\')+LENGTH(\''.$v.'\')) like']='%,'.$v.',%';	
						$OR['Left(News.tag,LENGTH(\''.$v.'\'))']=$v;
					}
				}
				$conditions['OR']=$OR;
			}
			
			//$this->News->recursive=-1;
			
			$news=$this->News->find('all',array('conditions'=>$conditions,'order'=>'News.id desc','limit'=>3));
			//get controller
			foreach($news as $k=>$v){
				if(intval($v['News']['parent_id'])>0){
					//get parent name:
					$parent=$this->NewsCategory->findById(intval($v['News']['parent_id']));
					$parent=$this->NewsCategory->findById(intval($parent['NewsCategory']['parent_id']));
					if(!empty($parent))$news[$k]['News']['controller']=$parent['NewsCategory']['title'];
				}	
			}
			$this->set('data',$data);
			$this->set('nextImages',$nextImages);
			$this->set('prevImages',$prevImages);
			$this->set('locale',$locale);
			$this->set('news',$news);
			//for seo
			if(!empty($data[$this->uses[0]]['title']))$catname=$data[$this->uses[0]]['title'];
			$this->set('metakeyword',$data[$this->uses[0]]['tag']);
			$this->set('metadescription',$data[$this->uses[0]]['description']);
			$this->set('ogTitle',$catname);
			$this->set('ogType','article');
			$this->set('ogDescription',$data[$this->uses[0]]['description']);
			$this->set('ogUrl','/loves/detail/'.$data[$this->uses[0]]['id'].'/'.$locale);
			$this->set('ogImage',$data[$this->uses[0]]['image']);
			$this->set('title_for_layout',$catname);
			
			$this->set('css','home,life');
		}
		else{
			$this->index();
		}
		
		
	}
}
